package com.dzk.cp.c11x5.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dzk.cp.c11x5.catcher.CodeCatcherService;
import com.dzk.cp.c11x5.constant.C11x5Constants;
import com.dzk.cp.c11x5.pojo.GdR5;
import com.dzk.cp.c11x5.pojo.JxR5;
import com.dzk.cp.c11x5.pojo.R5;
import com.dzk.cp.c11x5.pojo.SdR5;
import com.dzk.cp.c11x5.service.R5Service;
import com.dzk.cp.util.CpUtils;
import com.dzk.cp.util.DateUtils;
import com.dzk.cp.util.PropsUtils;

public class InitServlet extends HttpServlet {
	
	private static final long serialVersionUID = -2872603541393883869L;
	
	private static final Logger log = Logger.getLogger(InitServlet.class);

	public void init() throws ServletException {
		super.init();
		log.info("检验历史数据是否完整......");
		//启动时检验历史数据是否完整，若不完整则加载历史数据
		checkHistoryData();
		log.info("检验完毕...end");
	}

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//手动检验历史数据是否完整，若不完整则加载历史数据
		//清空历史数据，重新加载一边
		//--》》检验数据准确性
	}
	
	@SuppressWarnings("unchecked")
	private void checkHistoryData() {
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		String toDateStr = DateUtils.dateToStr(new Date());
		
		log.info("loading sd11x5:");
		R5Service<SdR5> sdR5Service = (R5Service<SdR5>) ctx.getBean("sdR5Service");
		CodeCatcherService<SdR5> sdCodeCatcherService = (CodeCatcherService<SdR5>) ctx.getBean("sdCodeCatcherService");
		loadHistoryData(sdR5Service, sdCodeCatcherService, 
				PropsUtils.getProperty(C11x5Constants.SD11X5_QUERY_BYDATE_FROM), toDateStr, 
				Integer.valueOf(PropsUtils.getProperty(C11x5Constants.SD11X5_TIMES)));
		
		log.info("loading jx11x5:");
		R5Service<JxR5> jxR5Service = (R5Service<JxR5>) ctx.getBean("jxR5Service");
		CodeCatcherService<JxR5> jxCodeCatcherService = (CodeCatcherService<JxR5>) ctx.getBean("jxCodeCatcherService");
		loadHistoryData(jxR5Service, jxCodeCatcherService, 
				PropsUtils.getProperty(C11x5Constants.JX11X5_QUERY_BYDATE_FROM), toDateStr, 
				Integer.valueOf(PropsUtils.getProperty(C11x5Constants.JX11X5_TIMES)));
		
		log.info("loading gd11x5:");
		R5Service<GdR5> gdR5Service = (R5Service<GdR5>) ctx.getBean("gdR5Service");
		CodeCatcherService<GdR5> gdCodeCatcherService = (CodeCatcherService<GdR5>) ctx.getBean("gdCodeCatcherService");
		loadHistoryData(gdR5Service, gdCodeCatcherService, 
				PropsUtils.getProperty(C11x5Constants.GD11X5_QUERY_BYDATE_FROM), toDateStr, 
				Integer.valueOf(PropsUtils.getProperty(C11x5Constants.GD11X5_TIMES)));
		
		this.getServletContext().setAttribute("isLoaded", Boolean.TRUE);
		log.info("isLoaded已设为true，允许定时获取数据");
	}
	
	private <T extends R5> void loadHistoryData(R5Service<T> r5Service, CodeCatcherService<T> CodeCatcherService, 
			String fromDateStr, String toDateStr, int times) {
		T r5 = r5Service.getLastR5();
		if (r5 != null) {
			String lastIssue = r5.getQs();
			String tmpStr = lastIssue.substring(0, 8);
			int n = Integer.valueOf(lastIssue.substring(8));
			if (tmpStr.compareTo(toDateStr.replace("-", "")) < 0 && n < times) {
				String issues = "";
				for (int i = n + 1; i <= times; i++) {
					issues += tmpStr + CpUtils.fore2zero(i) + ",";
				}
				issues = issues.substring(0, issues.length() - 1);
				log.info("加载历史数据ByIssue：issues=" + issues);
				CodeCatcherService.catchCodeByIssue(issues);
			}
			StringBuffer sb = new StringBuffer(tmpStr).insert(4, "-").insert(7, "-");
			fromDateStr = DateUtils.addDateStr(sb.toString(), 1);
		}
		
		log.info("加载历史数据ByDate：fromDate=" + fromDateStr + ", toDate=" + toDateStr);
		while (fromDateStr.compareTo(toDateStr) <= 0) {
			log.info("->" + fromDateStr + "...");
			CodeCatcherService.catchCodeByDate(fromDateStr);
			fromDateStr = DateUtils.addDateStr(fromDateStr, 1);
		}
	}

}
